package lesson04;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class UrlDown {
    public static void main(String[] args) throws Exception {
        //1.下载地址
        URL url = new URL("http://web.198game.com/mxsg/login_bg.jpg");

        //2.连接到这个资源 HTTP
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        //3.输入流
        InputStream inputStream = urlConnection.getInputStream();
        //4.下载到存放地址
        FileOutputStream fos = new FileOutputStream("mengxiangsanguo.jpg");
        //5.写出数据
        byte[] buffer = new byte[1024];
        int len ;
        while((len = inputStream.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }
        fos.close();
        inputStream.close();
        urlConnection.disconnect(); //断开连接

    }
}
